
const {ccclass, property} = cc._decorator;

@ccclass
export default class TimerManager {
	private timers:Array<ITimer> = new Array<ITimer>();//1秒运行一次
	private timers2:Array<ITimer> = new Array<ITimer>();//100毫秒运行一次
	private static _ins:TimerManager = null;
	private runCount:number = 0;
	public static get ins():TimerManager
	{
		if(!this._ins) this._ins = new TimerManager();
		return this._ins;
	}
    public constructor() 
    {
	}

	public run():void
	{
		this.runCount++;
		//秒运动
		if(this.runCount >= 10)
		{
			for(let i:number = 0, len:number = this.timers.length; i < len; i++)
			{
				this.timers[i].runTimer();
			}
			this.runCount = 0;
		}
		//毫秒运动
		for(let i:number = 0, len:number = this.timers2.length; i < len; i++)
		{
			this.timers2[i].runMilliTimer();
		}
	}

	public addTimer(_frame:ITimer):void
	{
		for(let i:number = 0, len:number = this.timers.length; i < len; i++)
		{
			if(this.timers[i] == _frame) return;
		}
		this.timers.push(_frame);
	}

	public removeTimer(_frame:ITimer):void
	{
		for(let i:number = 0, len:number = this.timers.length; i < len; i++)
		{
			if(this.timers[i] == _frame)
			{
				this.timers.splice(i, 1);
				break;
			}
		}
	}
	public addTimer2(_frame:ITimer):void
	{
		for(let i:number = 0, len:number = this.timers2.length; i < len; i++)
		{
			if(this.timers2[i] == _frame) return;
		}
		this.timers2.push(_frame);
	}

	public removeTimer2(_frame:ITimer):void
	{
		for(let i:number = 0, len:number = this.timers2.length; i < len; i++)
		{
			if(this.timers2[i] == _frame)
			{
				this.timers2.splice(i, 1);
				break;
			}
		}
	}
}

export interface ITimer
{
	runTimer():void;
	runMilliTimer():void;
}